<?php

// Prepare data for charts

$data = array();

foreach ($rawProfilingEvents as $rawProfilingEvent)
{
	//

	$profilingEvent->loadFromRawObject($rawProfilingEvent);


	//

	$data[] = 'data.addRow([' . $profilingEvent->getId() . ',' . round($profilingEvent->getDuration() * 100, 2) . ',' . round($profilingEvent->getMaxMemory() / 1024000, 2) . ']);';
}

?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
	google.load
	(
		'visualization',
		'1',
		{
			packages:['corechart']
		}
	);

	google.setOnLoadCallback(drawCharts);

	function drawCharts()
	{
		//

		var data = new google.visualization.DataTable();
		
		data.addColumn('number', 'ID');
		data.addColumn('number', 'Duration');
		data.addColumn('number', 'Memory');

		<?php p(implode("\n", $data)); ?>

		new google.visualization.LineChart(document.getElementById('chart1')).draw
		(
			data,
			{
				hAxis:
				{
					textColor: '#ffffff'
				},
				legend:
				{
					position: 'bottom',
					textStyle:
					{
						fontSize: 12
					}
				},
				pointSize: 10,
				series:
				{
					0: {targetAxisIndex:0},
					1: {targetAxisIndex:1}
				},
				vAxes:
				{
					0:
					{
						format: '#.## ms',
						logScale: false,
						minValue: 0,
						textStyle:
						{
							fontSize: 12
						}
					},
					1:
					{
						format: '#.## MB',
						logScale: false,
						minValue: 0,
						textStyle:
						{
							fontSize: 12
						}
					}
				}
			}
		);
	}
</script>
<div class="tabbable">
	<ul class="nav nav-tabs">
		<li class="active">
			<a href="#summary" data-toggle="tab">Summary</a>
		</li>
		<li>
			<a href="#events" data-toggle="tab">Events <span class="badge"><?php p(count($rawProfilingEvents)); ?></span></a>
		</li>
		<li>
			<a href="#request" data-toggle="tab">Request</a>
		</li>
		<li>
			<a href="#cookiesSession" data-toggle="tab">Cookies &amp; Session</a>
		</li>
		<li>
			<a href="#preferences" data-toggle="tab">Preferences</a>
		</li>
		<li>
			<a href="#php" data-toggle="tab">PHP Info</a>
		</li>
		<?php

		if (count($rawProfilingThreads) > 0)
		{

		?>
		<li class="dropdown pull-right">
			<a class="dropdown-toggle" data-toggle="dropdown" href="#">
				<strong><?php p($currentProfilingThread->getPathToView()); ?></strong>
				<strong class="caret"></strong>
			</a>
			<ul class="dropdown-menu">
				<?php

				foreach ($rawProfilingThreads as $rawProfilingThread)
				{
					$profilingThread->loadFromRawObject($rawProfilingThread);

					$class = null;

					if ($currentProfilingThread->getId() === $profilingThread->getId())
					{
						$class = 'active';
					}


				?>
				<li class="<?php p($class); ?>">
					<a href="?profilingThreadId=<?php p($profilingThread->getId()); ?>&amp;profilingThreadReference=<?php p($profilingThread->getReference()); ?>">
						<?php p($profilingThread->getPathToView()); ?>
					</a>
				</li>
				<?php

				}

				?>
			</ul>
		</li>
		<?php

		}

		?>
	</ul>
	<?php

	$duration = app()->getStringHelper()->formatDuration($currentProfilingThread->getDuration());
	$memory = app()->getStringHelper()->formatMemory($currentProfilingThread->getMaxMemory());

	?>
	<div class="tab-content">
		<div class="tab-pane active" id="summary">
			<div class="row-fluid">
				<div class="well well-small span12">
					<div class="span4 dashboard-figure">
						<h1><?php p($duration['value']); ?> <small><?php p($duration['metric']); ?></small></h1>
						<p>Duration</p>
					</div>
					<div class="span4 dashboard-figure">
						<h1><?php p($memory['value']); ?> <small><?php p($memory['metric']); ?></small></h1>
						<p>Memory Peak</p>
					</div>
					<div class="span4 dashboard-figure">
						<h1><?php p($currentProfilingThread->getNbQueries(), FORMAT_NUMBER); ?> <small>SQL</small></h1>
						<p>Queries</p>
					</div>
				</div>
			</div>
			<div class="well well-white">
				<div class="row-fluid">
					<div class="span1" style="line-height: 50px; text-align: right;"><strong>Duration</strong></div>
					<div class="progress span11" style="height: 50px; margin-bottom: 0px;">
						<?php

						//

						foreach ($rawProfilingEvents as $rawProfilingEvent)
						{
							//

							$profilingEvent->loadFromRawObject($rawProfilingEvent);


							//

							$duration = app()->getStringHelper()->formatDuration($profilingEvent->getDuration());
							$percentage = round(($profilingEvent->getDuration() / $currentProfilingThread->getDuration()) * 100);

							if ($percentage < 10)
							{
								$class = 'success';
							}
							else if ($percentage < 30)
							{
								$class = 'warning';
							}
							else
							{
								$class = 'danger';
							}

						?>
						<div	class="bar bar-<?php p($class); ?>"
								style="width: <?php p($percentage); ?>%;"
								rel="tooltip"
								data-original-title="<?php p($profilingEvent->getType()); ?>: <?php p($duration['value']); ?> <?php p($duration['metric']); ?> (<?php p($percentage); ?>%)">
						</div>
						<?php

						}

						?>
					</div>
				</div>
			</div>
			<div class="well well-white">
				<div id="chart1"></div>
			</div>
		</div>
		<div class="tab-pane" id="events">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th class="span1">Type</th>
						<th class="span10">Input / Output</th>
						<th class="span1">Duration</th>
					</tr>
				</thead>
				<tbody>
					<?php

					foreach ($rawProfilingEvents as $rawProfilingEvent)
					{
						//

						$profilingEvent->loadFromRawObject($rawProfilingEvent);


						//

						$input = $profilingEvent->getInput();
						$output = $profilingEvent->getOutput();
						$duration = app()->getStringHelper()->formatDuration($profilingEvent->getDuration());


						//

						$percentage = round(($profilingEvent->getDuration() / $currentProfilingThread->getDuration()) * 100);

						if ($percentage < 10)
						{
							$class = null;
						}
						else if ($percentage < 30)
						{
							$class = 'warning';
						}
						else
						{
							$class = 'error';
						}

					?>
					<tr class="<?php p($class); ?>" data-profiling-event-step="<?php p($profilingEvent->getStep()); ?>" data-profiling-event-type="<?php p($profilingEvent->getType()); ?>">
						<td><?php p($profilingEvent->getType()); ?></td>
						<td>
							<?php

							if (empty($input) === false)
							{

							?>
							<pre><small><?php p($input); ?></small></pre>
							<?php

							}

							?>
							<?php

							if (empty($output) === false)
							{

							?>
							<pre><small><?php p($output); ?></small></pre>
							<?php

							}

							?>
						</td>
						<td><?php p($duration['value']); ?><?php p($duration['metric']); ?></td>
					</tr>
					<?php

					}

					?>
				</tbody>
			</table>
		</div>
		<div class="tab-pane" id="request">
			<?php

			$sections = array
			(
				'GET' => json_decode($currentProfilingThread->getGetVariables()),
				'POST' => json_decode($currentProfilingThread->getPostVariables()),
				'HTTP Request Headers' => json_decode($currentProfilingThread->getHttpRequestVariables()),
				'HTTP Response Headers' => json_decode($currentProfilingThread->getHttpResponseVariables())
			);

			foreach ($sections as $sectionName => $sectionVariables)
			{
				if (is_object($sectionVariables) === true)
				{

			?>
			<h4 style="text-align: center;"><?php p($sectionName); ?></h4>
			<br />
			<table class="table table-bordered">
				<thead>
					<tr>
						<th class="span3">Name</th>
						<th class="span9">Value</th>
					</tr>
				</thead>
				<tbody>
					<?php

					foreach ($sectionVariables as $variableName => $variableValue)
					{
						if (empty($variableValue) === true)
						{
							$variableValue = 'null';
						}

					?>
					<tr>
						<td><?php p($variableName); ?></td>
						<td>
							<pre><small><?php print_r($variableValue); ?></small></pre>
						</td>
					</tr>
					<?php

					}

					?>
				</tbody>
			</table>
			<br />
			<?php

				}
			}

			?>
		</div>
		<div class="tab-pane" id="cookiesSession">
			<?php

			$sections = array
			(
				'Cookies' => json_decode($currentProfilingThread->getCookieVariables()),
				'Session' => json_decode($currentProfilingThread->getSessionVariables())
			);

			foreach ($sections as $sectionName => $sectionVariables)
			{
				if
				(
					(is_array($sectionVariables) === true) ||
					(is_object($sectionVariables) === true)
				)
				{

			?>
			<h4 style="text-align: center;"><?php p($sectionName); ?></h4>
			<br />
			<table class="table table-bordered">
				<thead>
					<tr>
						<th class="span3">Name</th>
						<th class="span9">Value</th>
					</tr>
				</thead>
				<tbody>
					<?php

					foreach ($sectionVariables as $variableName => $variableValue)
					{
						if (empty($variableValue) === true)
						{
							$variableValue = 'null';
						}

					?>
					<tr>
						<td><?php p($variableName); ?></td>
						<td>
							<pre><small><?php print_r($variableValue); ?></small></pre>
						</td>
					</tr>
					<?php

					}

					?>
				</tbody>
			</table>
			<br />
			<?php

				}
			}

			?>
		</div>
		<div class="tab-pane" id="preferences">
			<?php

			$preferences = json_decode($currentProfilingThread->getPreferences());

			?>
			<table class="table table-bordered">
				<thead>
					<tr>
						<th class="span3">Name</th>
						<th class="span8">Value</th>
						<th class="span1"></th>
					</tr>
				</thead>
				<tbody>
					<?php

					if (is_object($preferences) === true)
					{
						foreach ($preferences as $preferenceName => $preferenceValue)
						{

					?>
					<tr>
						<td><?php p($preferenceName); ?></td>
						<td>
							<pre><small><?php print_r($preferenceValue->value); ?></small></pre>
						</td>
						<td style="text-align: center;">
							<?php

							if ($preferenceValue->isLocked === true)
							{

							?>
							<span class="icon icon-lock"></span>
							<?php

							}

							?>
						</td>
					</tr>
					<?php

						}
					}

					?>
				</tbody>
			</table>
		</div>
		<div class="tab-pane" id="php">
			<?php

			$phpInformations = getPhpInformations();

			if (is_array($phpInformations) === true)
			{
				foreach ($phpInformations as $sectionName => $sectionInformations)
				{

			?>
			<h4 style="text-align: center;"><?php p($sectionName); ?></h4>
			<br />
			<table class="table table-bordered">
				<thead>
					<tr>
						<th class="span3">Name</th>
						<th class="span9">Value</th>
					</tr>
				</thead>
				<tbody>
					<?php

					foreach ($sectionInformations as $sectionInformation)
					{

					?>
					<tr>
						<td><?php p($sectionInformation['name']); ?></td>
						<td>
							<?php

							foreach ($sectionInformation['value'] as $value)
							{

							?>
							<pre><small><?php p($value); ?></small></pre>
							<?php

							}

							?>
						</td>
					</tr>
					<?php

					}

					?>
				</tbody>
			</table>
			<br />
			<?php

				}
			}

			?>
		</div>
	</div>
</div>
